//
//  WLNavigationScrollView.m
//  LookNovel
//
//  Created by chuangqi on 2021/3/8.
//  Copyright © 2021 cq Co.,ltd. All rights reserved.
//

#import "WLNavigationScrollView.h"
#import "UIColor+WLGradient.h"
#import "WLCollectionView.h"
#import "WLNavigationScrolleCollectionViewCell.h"

/** 小横线的宽 */
#define lien_view_width  9
/** 小横线的高 */
#define lien_view_height  3

@interface WLNavigationScrollView ()<WLCollectionDelegate>

@property(nonatomic,strong) WLCollectionView *wlclass_collectionView;

@property(nonatomic,strong) UIView *wlclass_lien_view;

@end
@implementation WLNavigationScrollView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        [self addSubview:self.wlclass_collectionView];
        [self.wlclass_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self).mas_offset(0);
        }];
        //            [self.wlclass_collectionView addSubview:self.wlclass_lien_view];
        //            [self.wlclass_lien_view mas_makeConstraints:^(MASConstraintMaker *make) {
        //                make.left.mas_equalTo(self.wlclass_collectionView).mas_offset(0);
        //                make.bottom.mas_equalTo(self);
        //                make.size.mas_equalTo(CGSizeMake(lien_view_width, lien_view_height));
        //            }];
        [self initViewModel];
    }
    return self;
}

-(void)initViewModel{
    /** 监听当前数据变更 */
    [RACObserve(self.wlclass_view_model, nav_class_list) subscribeNext:^(id  _Nullable x) {
        if (x != nil){
            [self reloadView:x];
        }
    }];
    /** 联动页面修改 */
    [RACObserve(self.wlclass_view_model, select_index) subscribeNext:^(id  _Nullable x) {
        if (![self.wlclass_collectionView initReload]){
            NSInteger selectIndex = [x integerValue];
            [self changeSelectPage:selectIndex];
        }
    }];
    [RACObserve(self.wlclass_view_model, offset_progress) subscribeNext:^(id  _Nullable x) {
        if (![self.wlclass_collectionView initReload]){
            CGFloat offsetProgress = [x floatValue];
            [self changeLienOffsetProgress:offsetProgress selectIndex:self.wlclass_view_model.select_index];
        }
    }];
    
}
/** 内容视图刷新 */
-(void)reloadView:(NSArray<WLNavigationScrollModel *> *)nav_list{
    [self.wlclass_collectionView setAllSection:[self.wlclass_view_model getNavigationItem:nav_list]];
}

-(void)changeSelectPage:(NSInteger)index{
    if ([self.wlclass_collectionView initReload]){
        return;
    }
    WLBaseCollectionViewLayerSection *section = [[self.wlclass_collectionView getAllSection] firstObject];
    [section.item each:^(WLBaseCollectionViewLayerRow *item, NSInteger idx) {
        if ([item.data isKindOfClass:WLNavigationScrollModel.class]){
            WLNavigationScrollModel *model = item.data;
            model.isSelect = (idx == index);
        }
    }];
    [self.wlclass_collectionView reloadData];
    [self.wlclass_collectionView layoutIfNeeded];
    [self.wlclass_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
-(void)changeLienOffsetProgress:(CGFloat)offsetProgress selectIndex:(NSInteger)selectIndex{
    if ([self.wlclass_collectionView initReload]){
        return;
    }
    //    WLNavigationScrollModel *cellData = [self.wlclass_collectionView.collection_delegate.viewModel getCellData:[NSIndexPath indexPathForRow:selectIndex inSection:0]];
    //    if ([cellData isKindOfClass:WLNavigationScrollModel.class]){
    //        CGFloat diff = offsetProgress-selectIndex;
    //
    //        CGFloat change = offsetProgress * 100 + (100/2.f - lien_view_width/2.f);
    //        [self.wlclass_lien_view mas_updateConstraints:^(MASConstraintMaker *make) {
    //            make.left.mas_equalTo(self.wlclass_collectionView).mas_offset(change);
    //        }];
    //        [self layoutIfNeeded];
    //    }
}

#pragma mark -- 代理等
//点击cell的响应
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)model
             didIndexPath:(NSIndexPath *)indexPath{
    self.wlclass_view_model.select_index = indexPath.row;
}

#pragma mark -- 懒加载
-(WLCollectionView *)wlclass_collectionView{
    if(!_wlclass_collectionView){
        _wlclass_collectionView = [WLCollectionView initWithHorizontalDelegate:self];
    }
    return _wlclass_collectionView;
}
-(UIView *)wlclass_lien_view{
    if (!_wlclass_lien_view){
        _wlclass_lien_view = [[UIView alloc]init];
        _wlclass_lien_view.backgroundColor = [UIColor fromColor:HexColor(@"FF253A") toColor:HexColor(@"FF826B") length:lien_view_width isHorizontal:YES];
    }
    return _wlclass_lien_view;
}

-(WLNavigationScrollViewModel *)wlclass_view_model{
    if (!_wlclass_view_model){
        _wlclass_view_model = [[WLNavigationScrollViewModel alloc]init];
    }
    return _wlclass_view_model;
}

@end

